const root = {
    val: 14,
    left: {
        val: 7,
        left: {
            val: 3,
        },
        right: {
            val: 9,
        },
    },
    right: {
        val: 18,
        left: {
            val: 16,
        },
        right: {
            val: 19,
        },
    },
};

function deleteNode(root, val) {
    if (!root) {
        return root
    }
    if (root.val > val) {
        root.left = deleteNode(root.left, val)
        return root
    } else if (root.val < val) {
        root.right = deleteNode(root.right, val)
        return root
    } else {
        if (!root.left) {
            return root.right
        } else if (!root.right) {
            return root.left
        } else {
            let cur = root.right
            while (cur.left) {
                cur = cur.left
            }
            cur.left = root.left
            return root.right
        }
    }
}

let res = deleteNode(root, 7)
console.log(res);